package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.bo.adminbo.AdminDelBo;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.po.MarketStorage;
import com.cskaoyan.app.bean.po.MarketStorageExample;
import com.cskaoyan.app.mapper.MarketStorageMapper;
import com.cskaoyan.app.service.AdminStorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author kiggy
 * @since 2022/12/29 11:44
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService {

    @Autowired
    MarketStorageMapper storageMapper;
    @Override
    public PageInfo<MarketStorage> listPageAdmins(BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        marketStorageExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        List<MarketStorage> marketStorages = storageMapper.selectByExample(marketStorageExample);

        PageInfo<MarketStorage> marketStoragePageInfo = new PageInfo<>(marketStorages);

        return marketStoragePageInfo;
    }

    /**
     * 将marketstorage数据添加到数据库中
     * 从数据库中获得storage，因为在添加前，storage没有id
     * @param url
     * @param file
     * @param name
     * @return
     */
    @Override
    public MarketStorage createStorageData(String url, MultipartFile file, String name) {
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(name);
        marketStorage.setName(name);
        marketStorage.setType("image/"+file.getContentType());
        marketStorage.setSize((int) file.getSize());
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setDeleted(false);
        marketStorage.setUrl(url);
        storageMapper.insert(marketStorage);
        MarketStorageExample storageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andUrlEqualTo(url);
        List<MarketStorage> storages = storageMapper.selectByExample(storageExample);
        return storages.get(0);
    }

    @Override
    public void delAdminData(AdminDelBo adminDelBo) {
        // 先取出
        MarketStorage marketStorage = storageMapper.selectByPrimaryKey(adminDelBo.getId());
        // 再删除
        marketStorage.setDeleted(true);
        storageMapper.updateByPrimaryKeySelective(marketStorage);
    }

    /**
     * 新增storage对象——>abandon
     * @return
     */
    @Override
    @Transactional
    public MarketStorage createStorageData(File file) {

        String fileName = file.getName();
        String[] split = fileName.split("\\.");
        String filetype = split[split.length - 1];

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(fileName);
        marketStorage.setName(fileName);
        marketStorage.setType("image/"+filetype);
        marketStorage.setSize((int) file.length());
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setDeleted(false);
        marketStorage.setUrl("http://localhost:8083/"+fileName);

        storageMapper.insert(marketStorage);

        // 取出
        PageHelper.startPage(1,1);
        MarketStorageExample storageExample = new MarketStorageExample();
        storageExample.setOrderByClause("add_time desc");
        List<MarketStorage> storages = storageMapper.selectByExample(storageExample);
        PageInfo<MarketStorage> storagePageInfo = new PageInfo<>(storages);
        List<MarketStorage> list = storagePageInfo.getList();
        return list.get(0);
    }
}
